package com.objectoriented.tang.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
/**
 棋局类，统计玩家输赢以及平局的场次
 */
public class CheckBoard {
    public static final int LINES_NUMBER=15;
    private int [][] chessOwner=new int[LINES_NUMBER+1][LINES_NUMBER+1];


    public boolean isLegal(Chess chess) {
        int x=chess.getX();
        int y=chess.getY();
        if (x>=1&&x<=LINES_NUMBER&&y>=1&&y<=LINES_NUMBER&&chessOwner[x][y]==0)return  true;
        return false;
    }//判断是否可以合法落子

    public boolean isWin(int x,int y,int owner){
        int sum=0;
        for (int i = x-1; i >=1 ; i--) {
            if (chessOwner[i][y]==owner)sum++;
            else break;
        }//水平向左
        for (int i = x+1; i <=LINES_NUMBER ; i++) {
            if (chessOwner[i][y]==owner)sum++;
            else break;
        }//水平向右
        if (sum>=4)return true;
        sum=0;
        for (int i = y-1; i >=1 ; i--){
            if (chessOwner[x][i]==owner)sum++;
            else break;
        }//垂直向下
        for (int i = y+1; i <=LINES_NUMBER ; i++) {
            if (chessOwner[x][i]==owner)sum++;
            else break;
        }//垂直向上
        if (sum>=4)return true;
        sum=0;
        for (int i = x-1,j=y-1; i >=1&&j>=1 ; i--,j--){
            if (chessOwner[i][j]==owner)sum++;
            else break;
        }//斜对角线(左上)
        for (int i = x+1,j=y+1; i <=LINES_NUMBER&&j <=LINES_NUMBER ; i++,j++) {
            if (chessOwner[i][j]==owner)sum++;
            else break;
        }//斜对角线(右下)
        if (sum>=4)return true;
        sum=0;
        for (int i = x+1,j=y-1; i <=LINES_NUMBER&&j>=1 ; i++,j--){
            if (chessOwner[i][j]==owner)sum++;
            else break;
        } //反斜对角线(右上)
        for (int i = x-1,j=y+1; i >=1&&j <=LINES_NUMBER ; i--,j++) {
            if (chessOwner[i][j]==owner)sum++;
            else break;
        } //反斜对角线(左下)
        if (sum>=4)return true;
        return false;
    }//判断落子后是否获胜
    public void addChess(int x, int y, int owner) {
        chessOwner[x][y]=owner;
    }
}
